﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ksp.Controller
{
    public static class EventParser
    {
        public static String KeyWord = "EVENTS";
        private static String active = "active = ";
        private static String guiActive = "guiActive = ";
        private static String guiIcon = "guiIcon = ";
        private static String guiName = "guiName = ";
        private static String category = "category = ";
        private static String guiActiveUnfocus = "guiActiveUnfocused = ";
        private static String unfocusedRage = "unfocusedRange = ";
        private static String externalToEVAOnly = "externalToEVAOnly = ";
        private const string bracketOpen = "{";
        private const string bracketClose = "}";


        public static List<Model.Event> ParseEvent(List<String> lines)
        {
            List<Model.Event> events = new List<Model.Event>();
            bool iterate = true;
            int lineNo = 1; // skip first bracket from EVENTS-Tag
            int lineMax = lines.Count - 1;
            while (lineNo < lineMax && iterate)
            {
                string Line = lines[lineNo];
                if (Line == bracketOpen)
                {
                    Model.Event ev = new Model.Event();
                    ev.Title = lines[lineNo - 1]; // get Event Title
                    lineNo++; // and jump into content
                    while (lineNo < lineMax && Line != bracketClose)
                    {
                        Line = lines[lineNo];

                        if (Line.StartsWith(active))
                            ev.Active = Line.Replace(active, String.Empty);
                        if (Line.StartsWith(guiActive))
                            ev.GuiActive = Line.Replace(guiActive, String.Empty);
                        if (Line.StartsWith(guiIcon))
                            ev.GuiIcon = Line.Replace(guiIcon, String.Empty);
                        if (Line.StartsWith(guiName))
                            ev.GuiName = Line.Replace(guiName, String.Empty);
                        if (Line.StartsWith(category))
                            ev.Category = Line.Replace(category, String.Empty);
                        if (Line.StartsWith(guiActiveUnfocus))
                            ev.GuiActiveUnfocus = Line.Replace(guiActiveUnfocus, String.Empty);
                        if (Line.StartsWith(unfocusedRage))
                            ev.UnfocusedRage = Line.Replace(unfocusedRage, String.Empty);
                        if (Line.StartsWith(externalToEVAOnly))
                            ev.ExternalToEVAOnly = Line.Replace(externalToEVAOnly, String.Empty);

                        lineNo++;
                    }
                    events.Add(ev);
                }
                else
                    lineNo++;
            }
            return events;
        }
    }
}
